import fs from 'fs';
import { resolve } from 'path';

const main = () => {
  fs.copyFileSync(resolve('./LICENSE'), resolve('./dist/LICENSE'));
  fs.copyFileSync(resolve('./README.md'), resolve('./dist/README.md'));

  fs.stat(resolve('./CHANGELOG.md'), (err, data) => {
    if (!err && data.isFile()) {
      fs.copyFileSync(resolve('./CHANGELOG.md'), resolve('./dist/CHANGELOG.md'));
    }
  });

  let pkgTplData = fs.readFileSync(resolve('./scripts/package.tpl.json'));
  let pkgTplObj = JSON.parse(pkgTplData.toString());

  let pkgSrcData = fs.readFileSync(resolve('./package.json'));
  let pkgSrcObj = JSON.parse(pkgSrcData.toString());

  pkgTplObj.version = pkgSrcObj.version;
  pkgTplObj.peerDependencies = pkgSrcObj.peerDependencies;
  pkgTplObj.dependencies = pkgSrcObj.dependencies;

  fs.writeFileSync(resolve('./dist/package.json'), JSON.stringify(pkgTplObj, null, '  '));
};

main();
